
import {getLogin} from '../pages/apis/index'

export const $request = ((params = {}) => {
    return new Promise((resolve, reject) => {
        wx.showLoading({
            title: '加载中',
        })
        // 从本地缓存中获取 token
        const token = wx.getStorageSync('token');

        wx.request({
            url: 'http://127.0.0.1:8080/api/' + params.url,
            method: params.method,
            header: {
                token: token
            },
            data: params.data,
            success: (res) => {
                // console.log("发起网络请求成功", res);
                // 拦截器
                if (res.statusCode == 401) {
                    console.log("状态码401");
                    //  重新登陆
                    getLogin()
                }
                resolve(res)
            },
            fail: (res) => {
                console.log("发起登录请求失败", res);
                reject(res)
            },
            complete: () => {
                // console.log("登录请求结束了");
                wx.hideLoading()
            }
        })
    })
})